package com.scuec.trainingmanagementsys.controller;

import com.scuec.trainingmanagementsys.entity.Announcement;
import com.scuec.trainingmanagementsys.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/announcements")
@CrossOrigin
public class AnnouncementController {
    @Autowired
    private AnnouncementService announcementService;

    // 新增公告
    @PostMapping
    public Map<String, Object> addAnnouncement(@RequestBody Announcement announcement) {
        Map<String, Object> result = new HashMap<>();
        try {
            int count = announcementService.addAnnouncement(announcement);
            if (count > 0) {
                result.put("success", true);
                result.put("message", "新增成功");
                result.put("data", announcement);
            } else {
                result.put("success", false);
                result.put("message", "新增失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "新增失败: " + e.getMessage());
        }
        return result;
    }

    // 整表查询（含关联字段）
    @GetMapping
    public Map<String, Object> findAll() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Announcement> list = announcementService.findAll();
            result.put("success", true);
            result.put("message", "查询成功");
            result.put("data", list);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }

    // 按标题查询（含关联字段）
    @GetMapping("/search/title")
    public Map<String, Object> findByTitle(@RequestParam String title) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Announcement> list = announcementService.findByTitle(title);
            result.put("success", true);
            result.put("message", "查询成功");
            result.put("data", list);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }

    // 单条删除
    @DeleteMapping("/{title}")
    public Map<String, Object> deleteByTitle(@PathVariable String title) {
        Map<String, Object> result = new HashMap<>();
        try {
            int count = announcementService.deleteByTitle(title);
            if (count > 0) {
                result.put("success", true);
                result.put("message", "删除成功");
            } else {
                result.put("success", false);
                result.put("message", "删除失败：未找到对应公告");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除失败: " + e.getMessage());
        }
        return result;
    }

    // 批量删除
    @DeleteMapping("/deleteBatch")
    public Map<String, Object> deleteByTitles(@RequestBody List<String> titles) {
        Map<String, Object> result = new HashMap<>();
        try {
            int count = announcementService.deleteByTitles(titles);
            if (count > 0) {
                result.put("success", true);
                result.put("message", "成功删除 " + count + " 条公告");
            } else {
                result.put("success", false);
                result.put("message", "删除失败：未找到对应公告");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除失败: " + e.getMessage());
        }
        return result;
    }

    // 根据教师的teacherNumber查询公告
    @GetMapping("/teacher/{teacherNumber}")
    public Map<String, Object> findByTeacherNumber(@PathVariable String teacherNumber) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Announcement> list = announcementService.findByTeacherNumber(teacherNumber);
            result.put("success", true);
            result.put("message", "查询成功");
            result.put("data", list);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }
}